import axios from 'axios'
import {message as antdMessage} from 'antd'

const FETCHING = 'PRODUCT_FETCHING'
const FETED = 'PRODUCT_FETED'
const SAVEING = 'PRODUCT_SAVEING'
const SAVED = 'PRODUCT_SAVED'
const DELING = 'PRODUCT_DELING'
const DELED = 'PRODUCT_DELED'
const ERROR = 'PRODUCT_ERROR'
const MESSAGE = 'PRODUCT_MESSAGE'

const init = {
    pagination:{
        page:1,
        size:10,
        total:0
    },
    data:[],
    loading:false,
}

export function product(state=init, action) {
    switch(action.type) {
        case FETCHING:
        case SAVEING:
        case DELING:
            return {...state, loading:true}
        case ERROR:
        case MESSAGE:
            return {...state, loading:false}
        case FETED:
            return {...state, loading:false, data:action.data, pagination:{...state.pagination, total:action.total}}
        case SAVED:
            return state
        case DELED:
            return {...state, data:state.data.filter(v=>v.id!=action.id), loading:false}
        default:
            return state
    }
}

function error(error) {
    antdMessage.error(error)
    return {
        type: ERROR
    }
}

function message(msg) {
    antdMessage.info(msg)
    return {
        type: MESSAGE
    }
}

function fetching() {
    return {
        type: FETCHING
    }
}

function fetched(data, total) {
    return {
        type: FETED,
        data,total
    }
}

export function fetch(params) {
    return dispatch=>{
        dispatch(fetching())
        axios.post('/product/list', params).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    dispatch(fetched(res.data.data.rows, res.data.data.total))
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '获取失败'))
                }
            } else {
                dispatch(error('网络错误:'+res.status))
            }
        })
    }
}

function saveing() {
    return {
        type: SAVEING
    }
}

function saved() {
    antdMessage.info('保存成功')
    return {
        type: SAVED
    }
}

export function save(params) {
    return dispatch=>{
        dispatch(saveing())
        axios.post('/product/save', params).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    dispatch(saved())
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '保存失败'))
                }
            } else {
                dispatch(error('网络错误:'+res.status))
            }
        })
    }
}

function deling() {
    return {
        type: DELING
    }
}

function deled(id) {
    antdMessage.info('已删除')
    return {
        type: DELED,
        id
    }
}

export function del(packageId) {
    return dispatch=>{
        dispatch(deling())
        axios.post('/product/del', {packageId}).then(res=>{
            if (res.status == 200) {
                if (res.data.code == 1) {
                    dispatch(deled(packageId))
                } else {
                    dispatch(error(res.data.msg ? res.data.msg : '删除失败'))
                }
            } else {
                dispatch(error('网络错误:'+res.status))
            }
        })
    }
}